﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 15:47:19
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class AuthoritySystem:IAuthoritySystem
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(SystemID),0)+1
                               FROM [AuthoritySystem]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private AuthoritySystemInfo GetByReader(IDataReader dr)
         {
            AuthoritySystemInfo info = new AuthoritySystemInfo();
            object obj;
            obj=dr["SystemID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SystemID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["SystemCode"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SystemCode = obj.ToString();
            }
            obj=dr["SystemName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SystemName = obj.ToString();
            }
            obj=dr["SystemUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.SystemUrl = obj.ToString();
            }
            obj=dr["IsActive"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsActive = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["OrderNum"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrderNum = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["Remark"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Remark = obj.ToString();
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["UpdateTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdateTime = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            return info;
         }
		public int Insert(AuthoritySystemInfo info)
		{
        
        
            string strSql=@"INSERT INTO AuthoritySystem			
			(			
			[SystemCode],
			[SystemName],
			[SystemUrl],
			[IsActive],
			[OrderNum],
			[Remark],
			[CreatedDate],
			[UpdateTime]
			)
			 VALUES
			(			
			@SystemCode,
			@SystemName,
			@SystemUrl,
			@IsActive,
			@OrderNum,
			@Remark,
			@CreatedDate,
			@UpdateTime
			)
			 SET @SystemID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@SystemID",DbType.Int32, sizeof(Int32));  
            db.AddInParameter(dbc, "@SystemCode",DbType.String, info.SystemCode);
            db.AddInParameter(dbc, "@SystemName",DbType.String, info.SystemName);
            db.AddInParameter(dbc, "@SystemUrl",DbType.String, info.SystemUrl);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@OrderNum",DbType.Int32, info.OrderNum);
            db.AddInParameter(dbc, "@Remark",DbType.String, info.Remark);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdateTime",DbType.DateTime, info.UpdateTime.ToNullDbDateTime());

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"SystemID");
         
		}
		
		
        public void Update(AuthoritySystemInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE AuthoritySystem SET ");
            sbSql.AppendLine("[SystemCode] = @SystemCode,");
                
            sbSql.AppendLine("[SystemName] = @SystemName,");
                
            sbSql.AppendLine("[SystemUrl] = @SystemUrl,");
                
            sbSql.AppendLine("[IsActive] = @IsActive,");
                
            sbSql.AppendLine("[OrderNum] = @OrderNum,");
                
            sbSql.AppendLine("[Remark] = @Remark,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[UpdateTime] = @UpdateTime");
                
            sbSql.Append(" where  [SystemID] = @SystemID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@SystemID",DbType.Int32, info.SystemID);
            db.AddInParameter(dbc, "@SystemCode",DbType.String, info.SystemCode);
            db.AddInParameter(dbc, "@SystemName",DbType.String, info.SystemName);
            db.AddInParameter(dbc, "@SystemUrl",DbType.String, info.SystemUrl);
            db.AddInParameter(dbc, "@IsActive",DbType.Int32, info.IsActive);
            db.AddInParameter(dbc, "@OrderNum",DbType.Int32, info.OrderNum);
            db.AddInParameter(dbc, "@Remark",DbType.String, info.Remark);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UpdateTime",DbType.DateTime, info.UpdateTime.ToNullDbDateTime());
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int systemID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [AuthoritySystem]  ");
            strSql.Append("WHERE [SystemID]=@SystemID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@SystemID",DbType.Int32, systemID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<AuthoritySystemInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<AuthoritySystemInfo> list = new List<AuthoritySystemInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "AuthoritySystem");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    AuthoritySystemInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public AuthoritySystemInfo GetByID(int systemID)
		{
            Model.AuthoritySystemInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [AuthoritySystem] ");
            strSql.Append("WHERE [SystemID]=@SystemID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@SystemID",DbType.Int32, systemID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}